package ext.tianma.part.finishedProduct.processors;

import java.util.ArrayList;

import wt.csm.navigation.ClassificationNode;
import wt.method.RemoteAccess;

import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.components.forms.FormResultAction;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.misc.NmContext;


public class PutSelectClassifyProcessor implements RemoteAccess {

	@SuppressWarnings("deprecation")
	public static FormResult setSelect(NmCommandBean commandBean)
			throws Exception {
		FormResult result = new FormResult();
		ArrayList nmcs = commandBean.getSelected();
		StringBuffer inter_sb = new StringBuffer();
		StringBuffer display_sb = new StringBuffer();
		String id = "";
		if (nmcs.size() < 1) {
			throw new Exception("未做选择任何对象！");
		}else if (nmcs.size() > 1) {
			throw new Exception("只能选择一个分类！");
		}
		int index = 0;
		for (Object nmc : nmcs) {
			if (nmc instanceof NmContext) {
				NmContext nc = (NmContext) nmc;
				Object refObject = nc.getTargetOid().getRefObject();
				id = nc.getTargetOid().toString();
				id = id.substring(id.lastIndexOf("|")+1);
				index++;
				ClassificationNode node = (ClassificationNode)refObject;
				String dis = node.getName();
				if(index == 1){
					inter_sb.append(id);
					display_sb.append(dis);
				}else{
					inter_sb.append(";"+id);
					display_sb.append(";"+dis);
				}
			}
		}
		String inters = inter_sb.toString();
		String displays = display_sb.toString();
		result.setStatus(FormProcessingStatus.SUCCESS);
		result.setNextAction(FormResultAction.JAVASCRIPT);
		result.setJavascript("setSelectIntoinput(\""+inters+"\",\""+displays+"\")");
		return result;
	}

}
